package com.origin.utils;

import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class PackageUtils {
    private static final String TAG = PackageUtils.class.getSimpleName();

    public boolean is64BitApp(ApplicationInfo applicationInfo) {
        if (applicationInfo == null) return false;

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // android 5.0
            return false;
        }

        String sourcePath = applicationInfo.publicSourceDir;

        if (TextUtils.isEmpty(sourcePath)) {
            return false;
        }
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(sourcePath);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                String name = entry.getName();
                if (name.contains("../")) {
                    continue;
                }

                if ((name.startsWith("lib/arm64")||name.startsWith("lib/arm64-v8a")) && !entry.isDirectory()) {
                   return true;
                } else if ((name.startsWith("lib/armeabi")||name.startsWith("lib/armeabi-v7a")) && !entry.isDirectory()) {
                    return false;
                }
            }
        } catch (Exception e) {
            Log.d(TAG, " " + e);
        } finally {
            if (zipFile != null) {
                try {
                    zipFile.close();
                } catch (Exception e) {
                    // Ignore.
                }
            }
        }

        return false;

    }
}
